package com.kitchen.npc
{
	import com.kitchen.AbstractNpc;
	import com.kitchen.AbstractProxy;
	import com.kitchen.ClassLoader;
	import com.kitchen.http.KitchenProxy;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class NpcContainer extends Sprite{
		public static var BOUT_END:String = 'boutend';
		public static var BOUT_ING:String = 'bouting';
		public static var LOAD_OK:String = 'loadok';
		
		//状态
		private var state:int = 0;
		//服务客人数量
		private var guests:int = 0;
		
		//npc库
		private var npcls:Array;
		//npc 数据
		private var npcs:Array;
		//npc movice
		private var npcm:Array;
		//店铺数据
		private var shop:Object;
		
		private var npcNum:int;
		
		private var startTimer:Timer;
		
		public function NpcContainer(shop:Object, npcNum:int=3){
			super();
			this.npcs = new Array();
			this.npcls = new Array();
			this.npcm = new Array();
			this.startTimer = new Timer(2000);
			this.startTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			this.npcNum = npcNum;
			this.loadCls(shop.npcs);
			this.setShop(shop);
		}
		
		//设置店铺信息
		public function setShop(shop:Object):void{
			this.shop = shop;
			this.loadCls(shop.npcs);
		}
		
		//获取游戏状态
		public function getState():int{
			return this.state;
		}
		
		//开始
		public function start():void{
			if((this.shop.shop.nStock > 0)&&(this.state == 0)){
				this.npcNum = 3;
				this.state = 1;
				this.loadNpcData();
			}else{
				this.debug('已经开始');
			}
		}
		
		//定时
		private function timerHandler(event:Event):void{
			if(this.npcNum > 0){
				var n:Object = this.getNpcData();
				if(n != null){
					n.tox = 850 - (this.npcNum*170);
					this.getNpcMovie(n).goAhead();
					this.npcNum --;
				}
			}else{
				this.startTimer.stop();
			}
		}
		
		//加载npc数据
		private function loadNpcData():void{
			var proxy:KitchenProxy = KitchenProxy.getInstance();
			proxy.getShopNpcs(1, 131);
			proxy.addEventListener(AbstractProxy.E_OK, dloadedHandler);
			proxy.addEventListener(AbstractProxy.E_CERROR, errorHandler);
		}
		
		private function loadCls(npcs:Array):void{
			for each(var r:Object in npcs){
				this.loadNpcClass(r);
			}
		}
		
		private function loadNpcClass(npc:Object):void{
			if(this.npcls[npc.id] == null){
				var cl:ClassLoader = new ClassLoader(npc.sUrl, npc.sClass, npc.id);
				cl.addEventListener(ClassLoader.LOADED_OK, cloadedHandler);
				cl.addEventListener(ClassLoader.LOADED_ERROR, errorHandler);
			}
		}
		
		private function cloadedHandler(event:Event):void{
			var cl:ClassLoader = ClassLoader(event.target);
			if(this.npcls[cl.id] == null){
				this.npcls[cl.id] = cl.result;
			}
			cl.removeEventListener(ClassLoader.LOADED_OK, cloadedHandler);
			cl.removeEventListener(ClassLoader.LOADED_ERROR, errorHandler);
			cl = null;
		}
		
		private function dloadedHandler(event:Event):void{
			var proxy:KitchenProxy = KitchenProxy(event.target);
			var result:Object = proxy.result;
			proxy.removeEventListener(AbstractProxy.E_OK, dloadedHandler);
			proxy.removeEventListener(AbstractProxy.E_CERROR, errorHandler);
			proxy = null;
			if(result.code == 200){
				this.npcs = result.result.data;
				this.startTimer.start();
				this.dispatchEvent(new Event(LOAD_OK));
			}else{
				debug(result.code+':'+result.message);
			}
		}
		
		private function getNpcData():Object{
			var result:Object=null;
			for each(var r:Object in this.npcs){
				if(r.isBuy == 0){
					r.isBuy = 1;
					result = r;
					break;
				}
			}
			return result;
		}
		
		private function getNpcMovie(npc:Object):AbstractNpc{
			var tm:AbstractNpc = null;;
			for each(var m:AbstractNpc in this.npcm){
				if((m.getId() == npc.nNpcId) && (m.getStep() == 0)){
					tm = m;
					break;
				}
			}
			if(tm == null){
				tm = new this.npcls[npc.nNpcId];
				tm.addEventListener(AbstractNpc.NPC_BACK, backHandler);
				tm.addEventListener(AbstractNpc.NPC_STAND, standHandler);
				tm.addEventListener(AbstractNpc.NPC_END, endHandler);
				this.addChild(tm);
				this.npcm.push(tm);
			}
			tm.setConfig(npc);
			return tm;
		}
		
		private function backHandler(event:Event):void{
			var tnpc:AbstractNpc = AbstractNpc(event.target);
			this.setChildIndex(tnpc, 0);
			var npc:Object = this.getNpcData();
			if(npc != null){
				var olnpc:Object = tnpc.getNpcData();
				npc.tox = olnpc.tox;
				this.getNpcMovie(npc).goAhead();
			}
		}
		
		private function endHandler(event:Event):void{
			this.guests ++;
			this.npcs.length;
			if(this.guests >= this.npcs.length){
				this.state = 0;
				this.guests = 0;
				this.dispatchEvent(new Event(BOUT_END));
			}
		}
		
		private function standHandler(event:Event):void{
			var tnpc:AbstractNpc = AbstractNpc(event.target);
			this.setChildIndex(tnpc, this.numChildren-1);
			
		}
		
		private function errorHandler(event:Event):void{
			debug('NpcContainer.loadNpcCls.error');
		}
		
		private function debug(message:String):void{
			trace(message);
		}
	}
}